﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Library.Membership;
using Eacademy.Library.Actions;

namespace Eacademy.Areas.Mentor.Controllers
{
    [ProfileControllerFilter]
    public class AssignmentsController : Controller
    {
        //
        // GET: /Mentor/Home/
        private const int DefaultPageSize = 10;
        private IMentorRepository _mentorRepository;
        private IAssignmentRepository _assignmentRepository;
        private ISubjectRepository _subjectRepository;
        private ILessonRepository _lessonRepository;

        public AssignmentsController(IMentorRepository mentorRepository, IAssignmentRepository assignmentRepository,ISubjectRepository subjectRepository)
        {
            _mentorRepository = mentorRepository;
            _assignmentRepository = assignmentRepository;
            _subjectRepository = subjectRepository;
        }

        CustomMembershipProvider provider = (CustomMembershipProvider)Membership.Provider;

        public ActionResult Index(int id, int? page)
        {
            int currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
            AdminAssignmentListViewModel adminAssignmentListViewModel = new AdminAssignmentListViewModel();
            int totalcount = 0;
            List<AdminAssignmentsViewModel> assignmentList = _assignmentRepository.GetAssignmentsBySubjectId(id, (int)provider.User.UserId, currentPageIndex + 1, DefaultPageSize, out totalcount);
            adminAssignmentListViewModel.AssignmentsList = assignmentList.ToPagedList(currentPageIndex, DefaultPageSize, totalcount);
            adminAssignmentListViewModel.Subject = _subjectRepository.GetSubjectBySubjectId(id);
            return View(adminAssignmentListViewModel);
        }

        public ActionResult Add(int id)
        {
            AdminAssignmentsViewModel adminAssignmentsViewModel = new AdminAssignmentsViewModel();
            adminAssignmentsViewModel.SubjectId = id;
            return View(adminAssignmentsViewModel);
        }

        [HttpPost]
        public ActionResult Add(AdminAssignmentsViewModel adminAssignmentsViewModel)
        {
            if (_assignmentRepository.AddAssignment(adminAssignmentsViewModel, adminAssignmentsViewModel.SubjectId, (int)provider.User.UserId))
            {
                this.FlashDone("The assignment has been created successfully");
                return Redirect(String.Format("/mentor/assignments/list/{0}", adminAssignmentsViewModel.SubjectId));
            }
            return View(adminAssignmentsViewModel);
        }

        public ActionResult Details(int id, int subid)
        {
            AdminAssignmentsViewModel assignment = _assignmentRepository.GetAssignmentByAssignmentId(id);
            assignment.SubjectId = subid;
            //assignment.FileList = new List<string>(assignment.Files.Split(','));
            return View("Details", assignment);
        }

        public ActionResult Edit(int id)
        {
            AdminAssignmentsViewModel assignment = _assignmentRepository.GetAssignmentByAssignmentId(id);
            //assignment.FileList = new List<string>(assignment.Files.Split(','));
            return View(assignment);
        }

        [HttpPost]
        public ActionResult Edit(AdminLessonsViewModel adminLessonsViewModel, string filenames, string deletefiles)
        {

            adminLessonsViewModel.Files = filenames;
            if (_lessonRepository.UpdateLesson(adminLessonsViewModel))
            {
                this.FlashDone("The lesson has been updated");
            }
            else
            {
                this.FlashError("There was an error updating the lesson");
            }
            return Redirect(String.Format("/mentor/lessons/list/{0}", adminLessonsViewModel.SubjectId));
        }
    }

}
